package com.example.Cairodio;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;

public class BackgroundService extends Service {

	private static final int HELLO_ID = 1;
	boolean notify = false;

	Intent GPSService;
	String GPS_FILTER = "guc.action.GPS_LOCATION";
	BroadcastReceiver receiver;
	private CairodioDbAdapter dbHelper;

	public static double currentLongitude;
	public static double currentLatitude;

	public static final double LONGITUDE = 0;
	public static final double LATITUDE = 0;

	public void onCreate() {

		// GPSService = new Intent(this, GpsService.class);
		// startService(GPSService);

		// Get Database
		dbHelper = ((CairodioApplication) getApplication()).getAdapter();

		IntentFilter mainFilter = new IntentFilter(GPS_FILTER);
		receiver = new MyMainLocalReceiver(this);
		registerReceiver(receiver, mainFilter);
	}

	@Override
	public void onStart(Intent intent, int startId) {

		super.onStart(intent, startId);
		//Toast.makeText(this, "Background service started", Toast.LENGTH_SHORT).show();

	}

	@Override
	public IBinder onBind(Intent intent) {
		// We don't provide binding, so return null
		return null;
	}

	@Override
	public void onDestroy() {

		super.onDestroy();
		//Toast.makeText(this, "Background service destroyed",Toast.LENGTH_SHORT).show();
	}

	public void check(double longitude, double latitude) {
		if (dbHelper.getNearByPlacesName(latitude, longitude).size() > 0) {
			// Notification 1

			String ns = Context.NOTIFICATION_SERVICE;
			NotificationManager myNotificationManager = (NotificationManager) this
					.getSystemService(ns);
			int icon = R.drawable.android_vector;
			CharSequence tickerText = "Nearby Places !";
			long when = System.currentTimeMillis();
			Notification notification = new Notification(icon, tickerText, when);

			Context context = getApplicationContext();
			String contentTitle = getString(R.string.app_name);
			String contentText = getString(R.string.background_service_notification_msg);
			Intent notificationIntent = new Intent(BackgroundService.this,
					NearbyPlacesActivity.class);
			Bundle b = new Bundle();
			b.putDouble("Latitude", 29.9);
			b.putDouble("Longitude", 31.1);
			notificationIntent.putExtras(b);
			PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
					notificationIntent, 0);

			notification.flags |= Notification.FLAG_AUTO_CANCEL;
			notification.defaults |= Notification.DEFAULT_LIGHTS;
			notification.setLatestEventInfo(context, contentTitle, contentText,
					contentIntent);
			myNotificationManager.notify(HELLO_ID, notification);
		}
	}

	private class MyMainLocalReceiver extends BroadcastReceiver {

		BackgroundService backgroundService;

		public MyMainLocalReceiver(BackgroundService bs) {
			backgroundService = bs;
		}

		@Override
		public void onReceive(Context localContext, Intent callerIntent) {
			double latitude = callerIntent.getDoubleExtra("latitude", -1);
			double longitude = callerIntent.getDoubleExtra("longitude", -1);

			BackgroundService.currentLongitude = longitude;
			BackgroundService.currentLatitude = latitude;

			/*Toast.makeText(BackgroundService.this,
					"Current Longitude" + BackgroundService.currentLongitude,
					Toast.LENGTH_SHORT).show();
			Toast.makeText(BackgroundService.this,
					"Current Latitude" + BackgroundService.currentLatitude,
					Toast.LENGTH_SHORT).show();*/

			backgroundService.check(longitude, latitude);
		}

	}

}
